package fr.utbm.aiechat.structures;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import fr.utbm.aiechat.R;

public class ChannelAdapter extends ArrayAdapter<Channel>
{
	private Context context;
	private int textViewResourceId;
	
	public ChannelAdapter(Context context, int textViewResourceId)
	{
		super(context, textViewResourceId);
		this.context = context;
		this.textViewResourceId = textViewResourceId;
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        View row = convertView;
        ChannelHolder holder = null;
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(textViewResourceId, parent, false);
           
            holder = new ChannelHolder();
            holder.ChanName = (TextView)row.findViewById(R.id.chanName);
           
            row.setTag(holder);
        }
        else
        {
            holder = (ChannelHolder)row.getTag();
        }
        
        Channel chan = getItem(position);
        holder.ChanName.setText(chan.getName());
       
        return row;
    }
   
    static class ChannelHolder
    {
        TextView ChanName;
    }
    
    public boolean chanExist(Channel c)
    {
    	for(int i = 0; i < getCount(); i++)
    	{
    		Channel chan = getItem(i);
    		if(c.getUniqueID() == chan.getUniqueID())
    		{
    			return true;
    		}
    	}
    	
    	return false;
    }
}
